{
 "cells": [
  {
   "cell_type": "code",
   "id": "initial_id",
   "metadata": {
    "collapsed": true,
    "ExecuteTime": {
     "end_time": "2025-09-01T09:17:52.003358Z",
     "start_time": "2025-09-01T09:17:51.885220Z"
    }
   },
   "source": "import numpy as np",
   "outputs": [],
   "execution_count": 1
  },
  {
   "metadata": {},
   "cell_type": "markdown",
   "source": "# 数组中满足条件的个数操作",
   "id": "67b3fd8d531e59e5"
  },
  {
   "metadata": {
    "ExecuteTime": {
     "end_time": "2025-09-01T09:37:15.829006Z",
     "start_time": "2025-09-01T09:37:15.823990Z"
    }
   },
   "cell_type": "code",
   "source": [
    "arr1 = np.array([[1, 3, 6], [9, 3, 2], [1, 4, 3]])\n",
    "print(f'原数组:\\n {arr1}')\n",
    "# print(f'大于3的元素的位置:\\n {arr1 > 3}')\n",
    "\n",
    "# 使用 np.where  满足条件 替换成520，不满足条件替换成1314\n",
    "# result = np.where(arr1 > 3,520,1314)\n",
    "# print('=' * 30)\n",
    "# print(result)\n",
    "\n",
    "result = np.where(arr1 > 3)\n",
    "for i in range(len(result[0])):\n",
    "    print(f\"({result[0][i] },{result[1][i]})\")\n",
    "\n",
    "\n"
   ],
   "id": "b6ca0c646b6fe10e",
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "原数组:\n",
      " [[1 3 6]\n",
      " [9 3 2]\n",
      " [1 4 3]]\n",
      "3\n"
     ]
    }
   ],
   "execution_count": 30
  },
  {
   "metadata": {},
   "cell_type": "markdown",
   "source": "## 2. 布尔值数组方法  any和all",
   "id": "13fdbec34e398c58"
  },
  {
   "metadata": {},
   "cell_type": "markdown",
   "source": "### 2.1 布尔值的统计",
   "id": "d59de21999a4ad50"
  },
  {
   "metadata": {
    "ExecuteTime": {
     "end_time": "2025-09-01T10:37:08.627640Z",
     "start_time": "2025-09-01T10:37:08.624397Z"
    }
   },
   "cell_type": "code",
   "source": [
    "arr2 = np.array([\n",
    "    [1, 3, 6],\n",
    "    [9, 3, 2],\n",
    "    [1, 4, 3]\n",
    "])\n",
    "print(f'原数组:\\n{arr2}')\n",
    "print(f'数组中大于3的元素个数:\\n{(arr2 > 3).sum()}')"
   ],
   "id": "9ca3f6515bd190a3",
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "原数组:\n",
      "[[1 3 6]\n",
      " [9 3 2]\n",
      " [1 4 3]]\n",
      "数组中大于3的元素个数:\n",
      "3\n"
     ]
    }
   ],
   "execution_count": 34
  },
  {
   "metadata": {},
   "cell_type": "markdown",
   "source": [
    "### 2.2. 对于布尔值数组，有两个常用方法any和all。\n",
    "- any：检查数组中是否至少有一个True\n",
    "- all：检查是否每个值都是True\n"
   ],
   "id": "b9d44de93a2822a9"
  },
  {
   "metadata": {
    "ExecuteTime": {
     "end_time": "2025-09-01T10:41:13.448385Z",
     "start_time": "2025-09-01T10:41:13.444979Z"
    }
   },
   "cell_type": "code",
   "source": [
    "arr3 = np.array([True,True,True,True])\n",
    "\n",
    "print(f'数组中是否有一个True：:\\n{arr3.any()}')\n",
    "print(f'数组中是否都是True：:\\n{arr3.all()}')"
   ],
   "id": "7b10d32970251368",
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "数组中是否有一个True：:\n",
      "True\n",
      "数组中是否都是True：:\n",
      "True\n"
     ]
    }
   ],
   "execution_count": 39
  },
  {
   "metadata": {
    "ExecuteTime": {
     "end_time": "2025-09-01T10:42:48.456919Z",
     "start_time": "2025-09-01T10:42:48.451265Z"
    }
   },
   "cell_type": "code",
   "source": [
    "arr4 = np.array([3,5,6,8])\n",
    "# 数组中是否有大雨3的元素\n",
    "print(f'数组中是否有大于3的元素:\\n{arr4[arr4 > 3].any()}')"
   ],
   "id": "eaa17d7780972610",
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "数组中是否有大于3的元素:\n",
      "True\n"
     ]
    }
   ],
   "execution_count": 41
  },
  {
   "metadata": {},
   "cell_type": "markdown",
   "source": [
    "## 3. 按值大小排序 sort\n",
    "- 语法：ndarray.sort(axis=-1, kind='quicksort', order=None)\n"
   ],
   "id": "d1795428e446465d"
  },
  {
   "metadata": {
    "ExecuteTime": {
     "end_time": "2025-09-01T10:46:14.271844Z",
     "start_time": "2025-09-01T10:46:14.268082Z"
    }
   },
   "cell_type": "code",
   "source": [
    "# 一维数组排序\n",
    "arr5 = np.array([3,5,6,9,8,4,2])\n",
    "arr5.sort()\n",
    "print(arr5)"
   ],
   "id": "fc31afe6d41a09bf",
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "[2 3 4 5 6 8 9]\n"
     ]
    }
   ],
   "execution_count": 42
  },
  {
   "metadata": {
    "ExecuteTime": {
     "end_time": "2025-09-01T10:49:18.152929Z",
     "start_time": "2025-09-01T10:49:18.147068Z"
    }
   },
   "cell_type": "code",
   "source": [
    "# 二维数组\n",
    "arr6 = np.array([\n",
    "    [1,3,9,2],\n",
    "    [3,6,8,7],\n",
    "    [2,5,4,1]\n",
    "])\n",
    "print(f'原数组:\\n{arr6} ')\n",
    "print('=' * 30)\n",
    "arr6.sort()\n",
    "\n",
    "print(f'排序后:\\n{arr6}')\n",
    "print('=' * 30)\n",
    "arr6.sort(axis=0)\n",
    "print(f'按照0轴排序后:\\n{arr6}')"
   ],
   "id": "e60e03ceca92fc52",
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "原数组:\n",
      "[[1 3 9 2]\n",
      " [3 6 8 7]\n",
      " [2 5 4 1]] \n",
      "==============================\n",
      "排序后:\n",
      "[[1 2 3 9]\n",
      " [3 6 7 8]\n",
      " [1 2 4 5]]\n",
      "==============================\n",
      "按照0轴排序后:\n",
      "[[1 2 3 5]\n",
      " [1 2 4 8]\n",
      " [3 6 7 9]]\n"
     ]
    }
   ],
   "execution_count": 45
  },
  {
   "metadata": {
    "ExecuteTime": {
     "end_time": "2025-09-01T11:01:17.700898Z",
     "start_time": "2025-09-01T11:01:17.696121Z"
    }
   },
   "cell_type": "code",
   "source": [
    "# 结构化数组\n",
    "dt = np.dtype([('name', 'S10'), ('age', int)])\n",
    "people = np.array([('Mike', 21), ('Nancy', 25), ('Bob', 17), ('Jane', 27)], dtype=dt)\n",
    "\n",
    "# 按照姓名排序\n",
    "# people.sort(order='age')\n",
    "people = np.sort(people,order='age')\n",
    "print(people)"
   ],
   "id": "8d695fe52247a13",
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "[(b'Bob', 17) (b'Mike', 21) (b'Nancy', 25) (b'Jane', 27)]\n"
     ]
    }
   ],
   "execution_count": 61
  },
  {
   "metadata": {},
   "cell_type": "markdown",
   "source": [
    "## 4.间接排序 argsort\n",
    "-  语法：numpy.argsort(a, axis=-1, kind='quicksort', order=None)\n",
    "- 对数组沿给定轴执行间接排序，并使用指定排序类型返回数据的索引数组。 这个索引数组用于构造排序后的数组。\n",
    "- 参数类似于sort()\n"
   ],
   "id": "1b44b7c3f6b02857"
  },
  {
   "metadata": {
    "ExecuteTime": {
     "end_time": "2025-09-01T10:59:22.304286Z",
     "start_time": "2025-09-01T10:59:22.297532Z"
    }
   },
   "cell_type": "code",
   "source": [
    "# 一维数组\n",
    "arr7 = np.array([3,5,6,9,8,4,2])\n",
    "# indexs = arr7.argsort()\n",
    "# print(f'排序后的索引:{indexs}') # [6 0 5 1 2 4 3]\n",
    "# print(f'排序后的数组:\\n{arr7[indexs]}')\n",
    "\n",
    "indexs = np.argsort(arr7)\n",
    "print(f'排序后的索引:{indexs}') # [6 0 5 1 2 4 3]\n",
    "print(f'排序后的数组:\\n{arr7[indexs]}')\n",
    "\n",
    "indexs = np.argsort(-arr7)\n",
    "print(f'降序排序后的索引:{indexs}') # [6 0 5 1 2 4 3]\n",
    "print(f'降序排序后的数组:\\n{arr7[indexs]}')\n",
    "\n"
   ],
   "id": "7293a68a4488f8e1",
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "排序后的索引:[6 0 5 1 2 4 3]\n",
      "排序后的数组:\n",
      "[2 3 4 5 6 8 9]\n",
      "降序排序后的索引:[3 4 2 1 5 0 6]\n",
      "降序排序后的数组:\n",
      "[9 8 6 5 4 3 2]\n"
     ]
    }
   ],
   "execution_count": 53
  },
  {
   "metadata": {
    "ExecuteTime": {
     "end_time": "2025-09-01T11:14:05.049944Z",
     "start_time": "2025-09-01T11:14:05.042037Z"
    }
   },
   "cell_type": "code",
   "source": [
    "# 二维数组\n",
    "arr8 = np.array([\n",
    "    [1,3,9,2],\n",
    "    [3,6,8,7],\n",
    "    [2,5,4,1]\n",
    "])\n",
    "print(f'原数组:\\n{arr8} ')\n",
    "print('=' * 30)\n",
    "index3 = arr8.argsort()\n",
    "print(f'排序后的数组下标:\\n{index3}')\n",
    "# # 通过下标获取二维数组的方法 take_along_axis\n",
    "sorted_arr = np.take_along_axis(arr8, index3, axis=1)\n",
    "print(\"\\n排序后的数组:\")\n",
    "print(sorted_arr)\n",
    "\n"
   ],
   "id": "6c8189bd08148a08",
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "原数组:\n",
      "[[1 3 9 2]\n",
      " [3 6 8 7]\n",
      " [2 5 4 1]] \n",
      "==============================\n",
      "排序后的数组下标:\n",
      "[[0 3 1 2]\n",
      " [0 1 3 2]\n",
      " [3 0 2 1]]\n",
      "\n",
      "排序后的数组:\n",
      "[[1 2 3 9]\n",
      " [3 6 7 8]\n",
      " [1 2 4 5]]\n"
     ]
    }
   ],
   "execution_count": 72
  },
  {
   "metadata": {},
   "cell_type": "markdown",
   "source": [
    "## 5. 根据键值的字典序进行排序 lexsort\n",
    "-  语法：lexsort(keys, axis=-1)\n",
    "\t-  lexsort()根据键值的字典序进行排序，支持对数组按指定行或列的顺序排序，间接排序，不修改原数组，返回索引。一般对一维数组使用argsort()。\n",
    "    - 默认按最后一行元素有小到大排序, 返回最后一行元素排序后索引所在位置。\n"
   ],
   "id": "a3446937db00f88d"
  },
  {
   "metadata": {
    "ExecuteTime": {
     "end_time": "2025-09-01T11:25:57.013475Z",
     "start_time": "2025-09-01T11:25:57.009926Z"
    }
   },
   "cell_type": "code",
   "source": [
    "arr9 = np.array([\n",
    "    [1,3,9,2],\n",
    "    [3,6,8,7],\n",
    "    [2,5,4,1]\n",
    "])\n",
    "\n",
    "# 是按照最后一个数组为主键进行排序的 [2,5,4,1]\n",
    "print(f'排序后的下标:\\n {np.lexsort(arr9)}')\n",
    "\n",
    "arr10 = np.array([1,3,5,2,76]) # 注意一维数组使用需要[] 包裹\n",
    "print(f'lexsort排序后的下标: {np.lexsort([arr10])}')\n"
   ],
   "id": "44a0910eb3aa857b",
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "排序后的下标:\n",
      " [3 0 2 1]\n",
      "lexsort排序后的下标: [0 3 1 2 4]\n"
     ]
    }
   ],
   "execution_count": 85
  },
  {
   "metadata": {
    "ExecuteTime": {
     "end_time": "2025-09-01T11:28:00.667848Z",
     "start_time": "2025-09-01T11:28:00.664230Z"
    }
   },
   "cell_type": "code",
   "source": [
    "a = np.array([1,3,2,8,7])\n",
    "b = np.array([2,6,9,7,1])\n",
    "\"\"\"\n",
    "    np.lexsort([a, b]) 的排序规则是：\n",
    "    先按最后一个数组 b 排序（主排序键）\n",
    "    如果 b 的值相同，再按前一个数组 a 排序（次排序键）\n",
    "    以此类推（如果有更多数组）\n",
    "\"\"\"\n",
    "ind = np.lexsort([a,b])\n",
    "print(ind)"
   ],
   "id": "a343b8c416436f31",
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "[4 0 1 3 2]\n"
     ]
    }
   ],
   "execution_count": 86
  },
  {
   "metadata": {
    "ExecuteTime": {
     "end_time": "2025-09-01T11:32:17.899206Z",
     "start_time": "2025-09-01T11:32:17.890381Z"
    }
   },
   "cell_type": "code",
   "source": [
    "\n",
    "# 创建学生数据\n",
    "学生姓名 = np.array(['Alice', 'Bob', 'Charlie', 'David', 'Eva'])\n",
    "数学成绩 = np.array([85, 92, 78, 92, 85])\n",
    "英语成绩 = np.array([90, 88, 95, 82, 92])\n",
    "年龄 = np.array([20, 19, 22, 21, 19])\n",
    "\n",
    "print(\"=== 学生成绩多键排序 ===\")\n",
    "print(\"原始数据:\")\n",
    "for i in range(len(学生姓名)):\n",
    "    print(f\"  {学生姓名[i]}: 数学{数学成绩[i]}, 英语{英语成绩[i]}, 年龄{年龄[i]}\")\n",
    "\n",
    "# 场景1: 先按数学成绩排序，数学相同按英语成绩排序\n",
    "sort_indices1 = np.lexsort((英语成绩, 数学成绩))  # 主键:数学, 次键:英语\n",
    "print(\"\\n1. 按数学成绩(主)→英语成绩(次)排序:\")\n",
    "for idx in sort_indices1:\n",
    "    print(f\"  {学生姓名[idx]}: 数学{数学成绩[idx]}, 英语{英语成绩[idx]}\")\n",
    "\n",
    "# 场景2: 先按年龄排序，年龄相同按数学成绩排序\n",
    "sort_indices2 = np.lexsort((数学成绩, 年龄))  # 主键:年龄, 次键:数学\n",
    "print(\"\\n2. 按年龄(主)→数学成绩(次)排序:\")\n",
    "for idx in sort_indices2:\n",
    "    print(f\"  {学生姓名[idx]}: 年龄{年龄[idx]}, 数学{数学成绩[idx]}\")"
   ],
   "id": "ee7f4cd29650d230",
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "=== 学生成绩多键排序 ===\n",
      "原始数据:\n",
      "  Alice: 数学85, 英语90, 年龄20\n",
      "  Bob: 数学92, 英语88, 年龄19\n",
      "  Charlie: 数学78, 英语95, 年龄22\n",
      "  David: 数学92, 英语82, 年龄21\n",
      "  Eva: 数学85, 英语92, 年龄19\n",
      "\n",
      "1. 按数学成绩(主)→英语成绩(次)排序:\n",
      "  Charlie: 数学78, 英语95\n",
      "  Alice: 数学85, 英语90\n",
      "  Eva: 数学85, 英语92\n",
      "  David: 数学92, 英语82\n",
      "  Bob: 数学92, 英语88\n",
      "\n",
      "2. 按年龄(主)→数学成绩(次)排序:\n",
      "  Eva: 年龄19, 数学85\n",
      "  Bob: 年龄19, 数学92\n",
      "  Alice: 年龄20, 数学85\n",
      "  David: 年龄21, 数学92\n",
      "  Charlie: 年龄22, 数学78\n"
     ]
    }
   ],
   "execution_count": 87
  },
  {
   "metadata": {
    "ExecuteTime": {
     "end_time": "2025-09-01T11:33:33.924544Z",
     "start_time": "2025-09-01T11:33:33.919826Z"
    }
   },
   "cell_type": "code",
   "source": [
    "# 创建二维数组（销售数据）\n",
    "sales_data = np.array([\n",
    "    [3, 1, 4],  # 产品A: 1月,2月,3月销量\n",
    "    [1, 5, 9],  # 产品B\n",
    "    [2, 6, 5]   # 产品C\n",
    "])\n",
    "产品名称 = np.array(['产品A', '产品B', '产品C'])\n",
    "\n",
    "print(\"\\n=== 销售数据排序 ===\")\n",
    "print(\"原始销售数据:\")\n",
    "print(sales_data)\n",
    "\n",
    "# 按3月份销量排序（第2列）\n",
    "sort_by_march = np.lexsort([sales_data[:, 2]])  # 按第3列排序\n",
    "print(\"\\n按3月份销量排序:\")\n",
    "for idx in sort_by_march:\n",
    "    print(f\"  {产品名称[idx]}: {sales_data[idx, 2]}份\")\n",
    "\n",
    "# 先按2月排序，2月相同按3月排序\n",
    "sort_indices = np.lexsort([sales_data[:, 2], sales_data[:, 1]])  # 主键:2月, 次键:3月\n",
    "print(\"\\n按2月(主)→3月(次)排序:\")\n",
    "for idx in sort_indices:\n",
    "    print(f\"  {产品名称[idx]}: 2月{sales_data[idx, 1]}份, 3月{sales_data[idx, 2]}份\")"
   ],
   "id": "74941dd7ec4ee363",
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "\n",
      "=== 销售数据排序 ===\n",
      "原始销售数据:\n",
      "[[3 1 4]\n",
      " [1 5 9]\n",
      " [2 6 5]]\n",
      "\n",
      "按3月份销量排序:\n",
      "  产品A: 4份\n",
      "  产品C: 5份\n",
      "  产品B: 9份\n",
      "\n",
      "按2月(主)→3月(次)排序:\n",
      "  产品A: 2月1份, 3月4份\n",
      "  产品B: 2月5份, 3月9份\n",
      "  产品C: 2月6份, 3月5份\n"
     ]
    }
   ],
   "execution_count": 88
  },
  {
   "metadata": {
    "ExecuteTime": {
     "end_time": "2025-09-01T11:35:06.388737Z",
     "start_time": "2025-09-01T11:35:06.385070Z"
    }
   },
   "cell_type": "code",
   "source": [
    "# 创建3D数组（多个店铺的销售数据）\n",
    "stores_sales = np.array([\n",
    "    [\n",
    "        [3, 1, 4],\n",
    "        [1, 5, 9]\n",
    "    ],  # 店铺1\n",
    "\n",
    "    [\n",
    "        [2, 6, 5],\n",
    "        [4, 8, 7]\n",
    "    ]   # 店铺2\n",
    "])\n",
    "\n",
    "print(\"\\n=== 多店铺销售数据排序 ===\")\n",
    "print(\"原始3D数据形状:\", stores_sales.shape)\n",
    "\n",
    "# 按每个店铺的最后一个月销量排序（axis=1）\n",
    "sort_indices = np.lexsort([stores_sales[:, :, 2]], axis=1)  # 按第3个月排序\n",
    "print(\"按最后一个月销量排序后的索引:\")\n",
    "print(sort_indices)"
   ],
   "id": "9b10233c2cf54794",
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "\n",
      "=== 多店铺销售数据排序 ===\n",
      "原始3D数据形状: (2, 2, 3)\n",
      "按最后一个月销量排序后的索引:\n",
      "[[0 1]\n",
      " [0 1]]\n"
     ]
    }
   ],
   "execution_count": 89
  },
  {
   "metadata": {
    "ExecuteTime": {
     "end_time": "2025-09-01T11:37:04.925472Z",
     "start_time": "2025-09-01T11:37:04.920671Z"
    }
   },
   "cell_type": "code",
   "source": [
    "# 员工数据排序\n",
    "员工号 = np.array([101, 102, 103, 104, 105])\n",
    "部门 = np.array([2, 1, 2, 1, 3])       # 部门编号\n",
    "薪资 = np.array([5000, 6000, 5500, 6500, 7000])\n",
    "入职年份 = np.array([2018, 2020, 2019, 2020, 2017])\n",
    "\n",
    "print(\"\\n=== 员工数据复杂排序 ===\")\n",
    "print(\"排序规则: 先按部门→再按薪资降序→最后按入职年份\")\n",
    "\n",
    "# 技巧: 要实现降序，可以用负数\n",
    "sort_indices = np.lexsort([入职年份, -薪资, 部门])  # 部门→薪资(降序)→年份\n",
    "\n",
    "print(\"排序结果:\")\n",
    "for idx in sort_indices:\n",
    "    print(f\"  员工{员工号[idx]}: 部门{部门[idx]}, 薪资{薪资[idx]}, 入职{入职年份[idx]}\")"
   ],
   "id": "aeed077e6d72d846",
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "\n",
      "=== 员工数据复杂排序 ===\n",
      "排序规则: 先按部门→再按薪资降序→最后按入职年份\n",
      "排序结果:\n",
      "  员工104: 部门1, 薪资6500, 入职2020\n",
      "  员工102: 部门1, 薪资6000, 入职2020\n",
      "  员工103: 部门2, 薪资5500, 入职2019\n",
      "  员工101: 部门2, 薪资5000, 入职2018\n",
      "  员工105: 部门3, 薪资7000, 入职2017\n"
     ]
    }
   ],
   "execution_count": 90
  },
  {
   "metadata": {
    "ExecuteTime": {
     "end_time": "2025-09-01T11:38:52.438531Z",
     "start_time": "2025-09-01T11:38:52.434046Z"
    }
   },
   "cell_type": "code",
   "source": [
    "# 有重复值的情况\n",
    "名字 = np.array(['张三', '李四', '王五', '赵六', '钱七'])\n",
    "分数 = np.array([85, 85, 90, 85, 90])  # 有重复分数\n",
    "完成时间 = np.array([30, 25, 35, 20, 40])  # 完成时间(分钟)\n",
    "\n",
    "print(\"\\n=== 比赛成绩排序 ===\")\n",
    "print(\"规则: 按分数降序，分数相同按完成时间升序(时间短的优先)\")\n",
    "\n",
    "# 分数降序: 用负数，完成时间升序: 正数\n",
    "sort_indices = np.lexsort([完成时间, -分数])\n",
    "\n",
    "print(\"排名:\")\n",
    "for i, idx in enumerate(sort_indices, 1):\n",
    "    print(f\"  第{i}名: {名字[idx]}, 分数{分数[idx]}, 时间{完成时间[idx]}分钟\")"
   ],
   "id": "af310281880c19e7",
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "\n",
      "=== 比赛成绩排序 ===\n",
      "规则: 按分数降序，分数相同按完成时间升序(时间短的优先)\n",
      "排名:\n",
      "  第1名: 王五, 分数90, 时间35分钟\n",
      "  第2名: 钱七, 分数90, 时间40分钟\n",
      "  第3名: 赵六, 分数85, 时间20分钟\n",
      "  第4名: 李四, 分数85, 时间25分钟\n",
      "  第5名: 张三, 分数85, 时间30分钟\n"
     ]
    }
   ],
   "execution_count": 91
  },
  {
   "metadata": {},
   "cell_type": "markdown",
   "source": "## 6.  唯一值与其它集合逻辑 unique和in1d",
   "id": "f012dfa3633cede5"
  },
  {
   "metadata": {
    "ExecuteTime": {
     "end_time": "2025-09-01T12:00:12.889871Z",
     "start_time": "2025-09-01T12:00:12.884883Z"
    }
   },
   "cell_type": "code",
   "source": [
    "# 去重操作\n",
    "# 去重操作\n",
    "names = np.array(['孙悟空', '猪八戒', '孙悟空', '沙和尚', '孙悟空', '唐僧'])\n",
    "names2,counts = np.unique(names,return_counts=True)\n",
    "print(names2)\n",
    "print(counts)\n",
    "\n",
    "print(list(zip(names2,counts)))"
   ],
   "id": "fffd7eb96a572fc4",
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "['唐僧' '孙悟空' '沙和尚' '猪八戒']\n",
      "[1 3 1 1]\n",
      "[(np.str_('唐僧'), np.int64(1)), (np.str_('孙悟空'), np.int64(3)), (np.str_('沙和尚'), np.int64(1)), (np.str_('猪八戒'), np.int64(1))]\n"
     ]
    }
   ],
   "execution_count": 122
  },
  {
   "metadata": {
    "ExecuteTime": {
     "end_time": "2025-09-01T11:45:54.183442Z",
     "start_time": "2025-09-01T11:45:54.177965Z"
    }
   },
   "cell_type": "code",
   "source": [
    "# 集合运算\n",
    "x = np.array([1, 2, 3, 4, 5])\n",
    "y = np.array([3, 4, 5, 6, 7])\n",
    "\n",
    "print(f'交集:{np.intersect1d(x, y)}')\n",
    "print(f'并集：{np.union1d(x, y)}')\n",
    "print(f'差集:{np.setdiff1d(x, y)}')\n",
    "print(f'对称差集:{np.setxor1d(x, y)}') # 对称差集是指两个集合中不属于它们交集的所有元素\n",
    "\n",
    "\n",
    "# 元素包含检查\n",
    "a = np.array([6, 0, 0, 3, 2, 5, 6])\n",
    "print(\"元素是否在[2,3,6]中:\", np.isin(a, [2, 3, 6]))"
   ],
   "id": "eb292ac85709097e",
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "交集:[3 4 5]\n",
      "并集：[1 2 3 4 5 6 7]\n",
      "差集:[1 2]\n",
      "对称差集:[1 2 6 7]\n",
      "元素是否在[2,3,6]中: [ True False False  True  True False  True]\n"
     ]
    }
   ],
   "execution_count": 97
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3 (ipykernel)",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 2
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython2",
   "version": "2.7.6"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 5
}
